import requests
import time
import json
import hashlib
import os


def back_sweep(num, sn, auth_code, token):
    url = "http://retail-device.lempay.cn/device/device_fs"
    data = {
        "sn": sn,
        "price": "1",
        "auth_code": auth_code,
        "version": "5.5.5",
        "token": token
    }
    rep = requests.post(url, data=data)
    res = rep.json()
    try:
        print(f"第{num + 1}次", res['msg'])
        # os.system('adb shell input keyevent 4')
        # time.sleep(0.4)
        # os.system('adb shell input tap 401 323')  # nubia
        # os.system('adb shell input tap 341 311')  # oppo
    except Exception:
        print("支付失败")
        print(json.dumps(res, indent=4, ensure_ascii=False))


def activation(sn):
    sign = check_code()
    url = 'http://retail-device.triumen.cn/device/check_status'
    data = {
        "sn": sn,
        "sign": sign,
        "version": "3.6.0",
        "time": round(time.time())
    }
    rep = requests.post(url, data=data)
    res = rep.json()
    return res
    # print(json.dumps(res, indent=4, ensure_ascii=False))


def check_code():
    timestamp = round(time.time())
    a = str(timestamp) + 'CMJF_getSignInfo'
    md5 = hashlib.md5()
    md5.update(a.encode('utf-8'))
    sign = md5.hexdigest()
    return sign


# def sweep():
#     os.system('adb shell am force-stop com.tencent.mm')
#     os.system('adb shell am start -n com.tencent.mm/com.tencent.mm.ui.LauncherUI')
#     time.sleep(2)
#     for num in range(300):
#         # os.system('ping -n 4 0.0.0.0 1>nul 2>nul')
#         # 点击扫一扫调起扫码
#         os.system('adb shell input tap 1002 168')
#         os.system('adb shell input tap 818 609')
#         # 点击图片，图库第一张二维码
#         os.system('adb shell input tap 937 1941')
#         time.sleep(0.5)
#         os.system('adb shell input tap 410 365')
#         # os.system('ping -n 3 0.0.0.0 1>nul 2>nul')
#         time.sleep(2)
#         # 输入0.01和付款
#         os.system('adb shell input tap 401 2182')
#         os.system('adb shell input tap 671 2182')
#         os.system('adb shell input tap 401 2182')
#         os.system('adb shell input tap 132 1642')
#         os.system('adb shell input tap 927 2003')
#         time.sleep(2)
#         # 点击确认付款
#         os.system('adb shell input tap 740 1350')
#         # os.system('ping -n 1 0.0.0.0 1>nul 2>nul')
#         time.sleep(1)
#         # 输入密码00001
#         os.system('adb shell input tap 836 825')
#         os.system('adb shell input tap 554 2206')
#         os.system('adb shell input tap 554 2206')
#         os.system('adb shell input tap 554 2206')
#         os.system('adb shell input tap 554 2206')
#         os.system('adb shell input tap 554 2206')
#         os.system('adb shell input tap 191 1743')
#         # os.system('ping -n 1 0.0.0.0 1>nul 2>nul')
#         time.sleep(2)
#         os.system('adb shell input keyevent 4')
#         print(f"第{num+1}次支付完成")
#         # os.system('adb shell input tap 997 125')
#         # os.system('adb shell input tap 834 593')
#         # time.sleep(10)
#         # os.system('adb shell input tap 400 1850')
#         # os.system('adb shell input tap 674 1850')
#         # os.system('adb shell input tap 400 1850')
#         # os.system('adb shell input tap 121 1349')
#         # os.system('adb shell input tap 747 1137')
#         # os.system('adb shell input tap 948 1689')
#         # time.sleep(1)
#         # os.system('adb shell input tap 539 1858')
#         # os.system('adb shell input tap 539 1858')
#         # os.system('adb shell input tap 539 1858')
#         # os.system('adb shell input tap 539 1858')
#         # os.system('adb shell input tap 539 1858')
#         # os.system('adb shell input tap 158 1381')
#         # time.sleep(1)
#         # os.system('adb shell input keyevent 4')
#         # time.sleep(0.5)


# i = str(input("请选择正扫还是反扫（1正扫2反扫）："))
# if i == "1":
#     sweep()
# if i == "2":
sn = str(input("请输入要反扫付款的sn："))
token = activation(sn)
num = str(input("请输入要反扫的次数："))
for num in range(int(num)):
    auth_code = str(input("输入付款码："))
    back_sweep(num, sn, auth_code, token)
